<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_aiagent/model')->方法名();
*/
namespace jy_aiagent;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	//获取聊天文本
	public function getmsg($content,$isexi=false) {
		$basic=$this->plugin_setting('basic');
		$apitype=$basic['apitype'];
		if(!$apitype){
			if(!$isexi){
				$res['choices'][0]['text']='chatAI暂时无法使用，请联系管理员，设置好基本参数：支持的AI';
				return $res;
			}else{
				exi('chatAI暂时无法使用，请联系管理员，设置好基本参数：支持的AI','error');
			}
		}
		$msg=model('jy_aiapi/model')->getmsg($content,$apitype,$isexi);
		if($msg){
			$this->upwenda($content,$msg);
		}
		return $msg;
	}
	public function getwenda($msg=''){
		$wenda=pdo_get('jy_aiagent_wenda',array('title like'=>'%'.trim($msg).'%','status'=>1,'uniacid'=>$_SESSION['uniacid']),'','paixu desc,id desc');
		return $wenda['msg'];
	}
	//机器人入库
	public function upwenda($title='',$msg=''){
		$wenda=pdo_get('jy_aiagent_wenda',array('title'=>trim($title),'uniacid'=>$_SESSION['uniacid']));
		if($wenda['id'] || !$title || !$msg){
			return false;
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'title'=>$title,
			'msg'=>$msg,
			'status'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_aiagent_wenda',$data);
		return true;
	}
	public function aicontent($news=array(),$isexi=false){
		$wenmsg=$news['wenmsg'];
		if($news['ntype']){
			$newstype=pdo_get('jy_aiagent_newstype',array('id'=>$news['ntype'],'uniacid'=>$_SESSION['uniacid']));
			$ptitle1=pdo_getcolumn('jy_aiagent_newstype',array('id'=>$newstype['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$ptitle2=$newstype['title'];
			$aineeds=$newstype['aineeds'];
		}else{
			$ptitle1=$news['isda']?$this->setting['ai_kemu2']:$this->setting['ai_kemu'];
			$ptitle2=$news['isda']?$this->setting['ai_kemutype2']:$this->setting['ai_kemutype'];
			$aineeds='无';
		}
		$params=array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_SESSION['bodyid']));
		$body=pdo_get('jy_aiagent_body',$params);
		if($body['id'] && !$body['xuetime']){
			//exi('请先设置小孩学龄段','error');
		}
		$xuetime=intval($body['xuetime']);
		$xuetimetitle=$this->xuetime[$xuetime];
		$ai_tomsg=$news['isda']?$this->setting['ai_tomsg']:$this->setting['ai_tomsg2'];
		$ai_tomsg=str_replace('[小孩提问内容]',$wenmsg,$ai_tomsg);
		$ai_tomsg=str_replace('[学龄段]','【'.$xuetimetitle.'】',$ai_tomsg);
		$ai_tomsg=str_replace('[科目]','【'.$ptitle1.'】',$ai_tomsg);
		$ai_tomsg=str_replace('[科目类型]','【'.$ptitle2.'】',$ai_tomsg);
		$ai_tomsg=str_replace('[解答要求]','【'.$aineeds.'】',$ai_tomsg);
		$ai_tomsg .='回答准确点，简短点！';
		return $ai_tomsg;
	}
	public function getbody_type(){
		$body_type=$this->body_type;
		$data=array();
		unset($body_type[0]);
		foreach($body_type as $k=>$title){
			if($k==1){
				$logo='../addons/jy_aiagent/resource/images/icon_nan.jpg';
				$logo2='../addons/jy_aiagent/resource/images/nan.png';
				$sex=1;
			}elseif($k==2){
				$logo='../addons/jy_aiagent/resource/images/icon_nv.jpg';
				$logo2='../addons/jy_aiagent/resource/images/nv.png';
				$sex=2;
			}elseif($k==3){
				$logo='../addons/jy_aiagent/resource/images/icon_nan1.jpg';
				$logo2='../addons/jy_aiagent/resource/images/nan2.png';
				$sex=1;
			}elseif($k==4){
				$logo='../addons/jy_aiagent/resource/images/icon_nv1.jpg';
				$logo2='../addons/jy_aiagent/resource/images/nv2.png';
				$sex=2;
			}elseif($k==5){
				$logo='../addons/jy_aiagent/resource/images/icon_other.jpg';
				$logo2='../addons/jy_aiagent/resource/images/nan2.png';
				$sex=1;
			}
			$data[$k]=array('key'=>$k,'title'=>$title,'logo'=>$logo,'logo2'=>$logo2,'sex'=>$sex);
		}
		return $data;
	}
}
?>